package com.ms.education.order.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.ms.education.JwtUtils;
import com.ms.education.R;
import com.ms.education.constant.OrderConstant;
import com.ms.education.exceptionHandler.EduException;
import com.ms.education.order.entity.Order;
import com.ms.education.order.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PutMapping("addOrder/{courseId}/{type}")
    public R addOrder(@PathVariable("courseId") Integer courseId,
                      @PathVariable("type") Integer type) {
        String orderNo = orderService.addOrder(courseId, Convert.toInt(StpUtil.getLoginId()), type);
        return R.ok().data("orderNo", orderNo);
    }

    @PutMapping("addOrderByClassId/{classId}/{type}")
    public R addOrderByClassId(@PathVariable("classId") Integer classId,
                               @PathVariable("type") Integer type) {

        String orderNo = orderService.addOrderByClassId(classId, Convert.toInt(StpUtil.getLoginId()), type);
        return R.ok().data("orderNo", orderNo);
    }

    @ApiOperation("根据订单号查询订单")
    @GetMapping("getOrderByOrderNo/{orderNo}")
    public R getOrderByOrderNo(@PathVariable("orderNo") String orderNo) {
        Order order = orderService.getOrderByOrderNo(orderNo);
        return R.ok().data("order", order);
    }

    @ApiOperation("查询订单状态")
    @GetMapping("getOrderStatus/{orderNo}")
    public R getOrderStatus(@PathVariable("orderNo") String orderNo) {
        Integer status = orderService.getOrderStatus(orderNo);
        return R.ok().data("status", status);
    }

    @ApiOperation("删除时间长且没有支付的订单")
    @DeleteMapping("deleteExpiredOrder")
    public R deleteExpiredOrder() {
        orderService.deleteExpiredOrder();
        return R.ok();
    }

    @ApiOperation("查询用户所有订单")
    @GetMapping("getAllOrdersOfUser")
    public R getAllOrdersOfUser() {
        List<Order> orders = orderService.getAllOrdersOfUser(Convert.toInt(StpUtil.getLoginId()));
        //分装数据
        List<Order> noOrders = orders.stream().filter(order -> order.getStatus() == OrderConstant.StatusEnum.STATUS_NO.getStatus()).collect(Collectors.toList());
        List<Order> yesOrders = orders.stream().filter(order -> order.getStatus() == OrderConstant.StatusEnum.STATUS_YES.getStatus()).collect(Collectors.toList());
        return R.ok().data("noOrders", noOrders).data("yesOrders", yesOrders);
    }

    @ApiOperation("查询订单是否已支付，已支付则删除未支付订单")
    @PostMapping("ifOrderPayAndDelete/{courseId}")
    public R ifOrderPayAndDelete(@PathVariable("courseId") Integer courseId) {
        boolean ifPay = orderService.ifOrderPay(courseId, Convert.toInt(StpUtil.getLoginId()));
        if (!ifPay) {
            return R.ok().data("ifPay", false).message("需要支付");
        } else {
            orderService.deleteNoPayOrder(courseId, Convert.toInt(StpUtil.getLoginId()));
            return R.ok().data("ifPay", true).message("该课程已付费，已为您删除无效订单");
        }
    }

}
